package study.mybatis.database.cpool.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class SqlSessionController {

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @RequestMapping("/sqlSession")
    public String user1(boolean close) {
        log.info("sssssss");
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try {
            sqlSession.selectOne("selectById", 1L);
        } finally {
            if (close) {
                log.info(">>>>>> close SqlSession");
                sqlSession.close();
            }
        }
        return "ok";
    }
}
